package com.kupurui.asteacher.ui;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.frame.ui.BaseFragment;
import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.AppManger;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.http.Banben;
import com.kupurui.asteacher.ui.index.IndexFgt;
import com.kupurui.asteacher.ui.job.JobFgt;
import com.kupurui.asteacher.ui.mine.MineFgt;
import com.kupurui.asteacher.ui.statistics.StatisticsFgt;
import com.kupurui.asteacher.update.DownloaderUtil;
import com.kupurui.asteacher.update.UpdateManager;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import okhttp3.Call;
import okhttp3.Response;


public class MainActivity extends BaseAty {

    @Bind(R.id.fgt_content)
    FrameLayout fgtContent;
    @Bind(R.id.radiobtn_index)
    RadioButton radiobtnIndex;
    @Bind(R.id.radiobtn_job)
    RadioButton radiobtnJob;
    @Bind(R.id.radiobtn_statistics)
    RadioButton radiobtnStatistics;
    @Bind(R.id.radiobtn_mine)
    RadioButton radiobtnMine;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;

    List<BaseFragment> fragments;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        JPushInterface.setAliasAndTags(this, UserManger.getId(), null, new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {

            }
        });


        fragments = new ArrayList<>();
        fragments.add(new IndexFgt());
        fragments.add(new JobFgt());
        fragments.add(new StatisticsFgt());
        fragments.add(new MineFgt());
        switchFragment(fragments.get(0));
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radiobtn_index:
                        switchFragment(fragments.get(0));
                        break;
                    case R.id.radiobtn_job:
                        switchFragment(fragments.get(1));
                        break;
                    case R.id.radiobtn_statistics:
                        showToast("该功能正在建设中。。。");
//                        switchFragment(fragments.get(2));
                        break;
                    case R.id.radiobtn_mine:
                        switchFragment(fragments.get(3));
                        break;
                }
            }
        });
    }

    @Override
    public void requestData() {
        new Banben().index(DownloaderUtil.getVersionCode(this)+"",this,0);
    }

    @Override
    public void onSuccess(final String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                String status = AppJsonUtil.getString(result, "status");
                if (status.equals("0")) {
//                showToast("已是最新版本!");
                } else {
                    final String versionCode = AppJsonUtil.getString(result, "version");
                    if (UserManger.getIgnoreVersion().equals(versionCode)) {
                        return;
                    }
                    if (AppJsonUtil.getString(result, "is_must").equals("0")) {
                        new AlertDialog.Builder(this).setTitle("有新的版本").setMessage(Html.fromHtml(AppJsonUtil.getString(result, "content"))).setNegativeButton("以后再说", null).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UpdateManager updateManager = new UpdateManager(MainActivity.this,AppJsonUtil.getString(result, "appurl"));
                                updateManager.showDownloadDialog();
                            }
                        }).setNeutralButton("忽略该版本", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserManger.setIgnoreVersion(versionCode);
                            }
                        }).create().show();
                    } else {
                        new AlertDialog.Builder(this).setTitle("有新的版本").setMessage(Html.fromHtml(AppJsonUtil.getString(result, "content"))).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UpdateManager updateManager = new UpdateManager(MainActivity.this,AppJsonUtil.getString(result, "appurl"));
                                updateManager.showDownloadDialog();
                            }
                        }).setCancelable(false).show();
                    }
                }
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void reSetFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Fragment loadFragment : loadFragments) {
            fragmentTransaction.hide(loadFragment);
        }
        fragmentTransaction.show(fragment);
        fragment.onResume();
        fragmentTransaction.commit();
    }


    private List<Fragment> loadFragments = new ArrayList<>();

    private void switchFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (loadFragments.contains(fragment)) {
            reSetFragment(fragment);
        } else {
            for (Fragment loadFragment : loadFragments) {
                fragmentTransaction.hide(loadFragment);
            }
            fragmentTransaction.add(R.id.fgt_content, fragment).commit();
            loadFragments.add(fragment);
        }
    }


    private long lastTime;
    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (System.currentTimeMillis() - lastTime < 3000) {
            AppManger.getInstance().killAllActivity();
        } else {
            showToast("连按两次退出app");
        }
        lastTime = System.currentTimeMillis();
    }
}
